from flask_classy import FlaskView, route
from ..models.product import Product
from ..schema.object import ProductSchema
from . import jsonify_with_success, jsonify_with_error


class ProductView(FlaskView):
    @route('/title/<title>')
    def view_by_title(self, title):
        product = Product.select().where(Product.title == title).get()
        if product:
            schema = ProductSchema()
            schema.context = {'language': 'zh'}
            return jsonify_with_success(schema.dump(product).data)
        else:
            return jsonify_with_error(202020, '产品未找到')
